/**
 * 2011-11-13下午8:09:33
 */
package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author Baicai 
 * 数据库连接工具类，对连接进行封装。 连接方式读取资源文件获取，使用时需指定要执行的sql语句。
 */
public final class MysqlConn {

	private static String urlString = null;//数据库连接url
	private static String usernameString = null;//数据库账号
	private static String passwordString = null;//数据库密码
	private static String sqlString = null;//执行的sql语句
	private static Connection connection = null;
	private static Statement statement = null;
	private static ResultSet resultSet = null;

	private MysqlConn() {
	}

	/**
     * 静态代码块注册连接数据库驱动
     */
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			throw new ExceptionInInitializerError(e);
		}
	}

	/**
	 * 读取资源文件获取url和用户名密码，获得数据库连接
	 * @return the connection
	 * @throws SQLException
	 * Connection
	 */
	public static Connection getConnection() throws SQLException {

		urlString = ReadProperties.readFile("url");
		usernameString = ReadProperties.readFile("username");
		passwordString = ReadProperties.readFile("password");

		if (urlString == null || usernameString == null
				|| passwordString == null) {
			return null;
		}
		return DriverManager.getConnection(urlString, usernameString,
				passwordString);
	}

	/**
	 * 查询数据库方式，返回结果集
	 * @return resultSet
	 * @throws SQLException
	 * ResultSet
	 */
	public static ResultSet queryDB() throws SQLException {
		if (sqlString == null) {
			return null;
		}
		connection = getConnection();
		statement = connection.createStatement();
		resultSet = statement.executeQuery(sqlString);
		return resultSet;
	}
	
	/**
	 * 查询刚刚插入的最后一条数据的id
	 * @return flag
	 * @throws SQLException
	 * int
	 */
	public static int selectLastDB() throws SQLException {
		int flag = 0;
		if (sqlString == null) {
			return 0;
		}
		connection = getConnection();
		statement = connection.createStatement();
		statement.executeUpdate(sqlString);//进行数据插入操作
		ResultSet resultSet = statement.executeQuery("select @@identity");//在关闭连接前执行，查询刚才插入商品的id
		resultSet.next();
		flag = resultSet.getInt(1);//取得这个id
		return flag;
	}

	
	/**
	 * 升级数据库，进行删改所用的操作，返回flag表示受到影响的行数
	 * @return flag
	 * @throws SQLException
	 * int
	 */
	public static int updateDB() throws SQLException {
		int flag = 0;
		if (sqlString == null) {
			return 0;
		}
		connection = getConnection();
		statement = connection.createStatement();
		flag = statement.executeUpdate(sqlString);

		return flag;
	}

	/**
	 * 释放数据库资源所进行的操作
	 * void
	 */
	public static void free() {
		try {
			if (resultSet != null)
				resultSet.close();
		} catch (SQLException sqlException) {
			sqlException.printStackTrace();
		} finally {
			try {
				if (statement != null)
					statement.close();
			} catch (SQLException sqlException) {
				sqlException.printStackTrace();
			} finally {
				if (connection != null)
					try {
						connection.close();
					} catch (SQLException sqlException) {
						sqlException.printStackTrace();
					}
			}
		}
	}

	/**
	 * @return the sqlString
	 */
	public static String getSqlString() {
		return sqlString;
	}

	/**
	 * @param sqlString
	 *            the sqlString to set
	 */
	public static void setSqlString(String sqlString) {
		MysqlConn.sqlString = sqlString;
	}

}
